<?php

namespace addons\booking\controller;

use addons\booking\model\StoreUser as StoreUserModel;
use addons\booking\model\Worker as WorkerModel;
use addons\booking\model\Areacity;
use addons\booking\model\WorkerAuth;

use fast\Tree;
use think\Db;
use think\Session;


class Cleaning extends Base
{
    protected $noNeedLogin = ['get_work_experience','get_citys'];
    protected $modelValidate = true; //是否开启Validate验证，默认是false关闭状态
    protected $modelSceneValidate = true; //是否开启模型场景验证，默认是false关闭状态

    //立即加入
    public function save_worker()
    {
        $user_id = $this->auth->id;

        $user_name = $this->request->param('user_name','');
        $work_experience = $this->request->param('work_experience','');
        $desire_area = $this->request->param('desire_area','');
        $nature = $this->request->param('nature/d',1);

        if(!$user_name)  $this->error('请填写姓名');
        if(!$work_experience)  $this->error('请选择工作经验');
        if(!$desire_area)  $this->error('请选择期望工作区域');

        $data = [
            'user_id'   =>  $user_id,
            'user_name' =>   $user_name,
            'work_experience' => $work_experience,
            'desire_area'  => $desire_area,
            'nature' => $nature,
            'create_time' => time(),
            'examine_time' => 0,
            'examine_status' => 0,
            'examine_content' => '',
        ];
        $worker = WorkerModel::where('user_id',$user_id)->find();
        $id = 0;
        if(!$worker){
            $res = $id =  WorkerModel::insertGetId($data);

        } else {
            $res = WorkerModel::where('id',$worker['id'])->update($data);
            $id = $worker['id'];
        }

        $worker = WorkerModel::where('user_id',$user_id)->find();

        if(!$res){
            $this->error('提交失败');
        } else{
            $this->success('提交成功',$worker);
        }
    }

    //获取保洁信息
    public function get_worker_info()
    {
        $worker_id = $this->request->param('worker_id','');
        if(!$worker_id)  $this->error('参数错误');

        $worker_info = WorkerModel::get_worker_info($worker_id);
        $this->success('',$worker_info);
    }

    //获取工作经验
    public function get_work_experience(){
        $data = [
            ['id'=>1,'title'=>'一年工作经验'],
            ['id'=>2,'title'=>'三年工作经验'],
            ['id'=>3,'title'=>'五年工作经验'],
            ['id'=>4,'title'=>'八年工作经验'],
            ['id'=>5,'title'=>'十年工作经验'],
        ];
        $this->success('',$data);
    }

    //获取开放区域
    public function get_citys()
    {
        $pid = $this->request->param('pid',0);
        $list = Areacity::where('pid',$pid)->select();
        $this->success('',$list);
    }


    //设置上线下线
    public function set_work_status()
    {
        $user_id = $this->auth->id;
        $worker = WorkerModel::where('user_id',$user_id)->find();
        if(!$worker)  $this->error('您还不是保洁');
        $res = WorkerModel::set_work_status($worker['id']);
        if(!$res){
            $this->error('提交失败');
        } else{
            $this->success('提交成功');
        }
    }
}
